package com.quicklife.service.impl;

import java.util.List;

import net.sf.json.JSONObject;

import com.quicklife.pojo.ActionLogs;
import com.quicklife.pojo.Business;
import com.quicklife.service.inter.GetBusInfosService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 获取某个商家详情/根据经纬度搜索周围商家
 * 
 * @author HackerD
 * 
 */
public class GetBusInfosServiceImpl implements GetBusInfosService {

	@Override
	public String getBusInfosById(String rqinfos) {
		List list = null;
		try {
			list = SqlHelper
					.executeQuery(
							"select a.ID,b.type typename,login_name,name,phone,"
									+ "address,logo,lat,lon,score,roleid,status from business a "
									+ "inner join buss_type b on a.type=b.id where a.id=?",
							new String[] { rqinfos }, Business.class);

		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}

		return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
	}

	@Override
	public String searchBusInfosByLatLon(String rqinfos) {
		JSONObject jsonObject = JSONObject.fromObject(rqinfos);
		List list = null;
		try {
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");
			double lon = jsonObject.getDouble("lon");
			double lat = jsonObject.getDouble("lat");
			list = SqlHelper
					.executeQuery(
							"select * from(select c.*,rownum rnum from "
									+ "(select a.ID,b.type,LOGIN_NAME,NAME,PHONE,ADDRESS,LOGO,"
									+ "DistanceOfTwoPoints(?,?,lon,lat) distance,SCORE,ROLEID,STATUS "
									+ "from business a "
									+ "inner join buss_type b on a.type=b.id "
									+ "order by distance) c) "
									+ "where  rnum>=? and rnum<?",
							new String[] { StringUtil.toString(lon),
									StringUtil.toString(lat),
									StringUtil.toString(begin),
									StringUtil.toString((begin + need)) }, null);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String getBusCommsById(String rqinfos) {
		List list = null;
		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);		
			int id = jsonObject.getInt("busid");
			int begin = jsonObject.getInt("begin");
			int need = jsonObject.getInt("need");

			list = SqlHelper
					.executeQuery(
							"select * from (select rownum rnum,f.* from (select b.nickname,"
									+ "b.photo uphoto,to_char(ac_time,'yyyy-MM-dd HH:mm:ss') ac_time,a.comments from action_logs a  "
									+ "inner join tuser b on a.userid=b.id "
									+ "where  a.ac_type=3 and a.bussid=? "
									+ "order by ac_time) f) "
									+ "where rnum>=? and rnum<?",
							new String[] { StringUtil.toString(id),
									StringUtil.toString(begin),
									StringUtil.toString((begin + need)) },
							null);

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}
}
